// pages/addDevList/addDevList.js
const app = getApp();
var util = require('../../utils/util.js');
var devArr = [];
Page({
  /**
   * 页面的初始数据
   */
  data: {
    devList:[{
      image:"../../images/logo.png",
      name: '壁挂炉HMJD-C025D - AKD',
      modelId:'dhcnco',
      isShow:1
    }],
    shareModelId:'',//分享的modelId
    qRCodeMsg:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var devVar = wx.getStorageSync('devList')
    if (devVar){;
      this.setData({
        devList: devVar
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var timestamp = new Date().getTime();
    var that=this;
    that.setData({
      devList:[]
    })
    wx.request({
      url: 'https://papi.zcyun.cn/v1/app/catalog/' + app.globalData.cengJiNode +'/list',
      data: {},
      method: "GET",
      header: {
        'content-type': 'application/json', // 默认值
        'openId': app.globalData.openId,
        'ts': timestamp,
        'sign': util.sha1("GET" + '/v1/app/catalog/' + app.globalData.cengJiNode + '/list' + timestamp + app.globalData.openKey)
      },
      success(res) {
        if(res.data.code==0){
          that.setData({
            devList: []
          })
          devArr = [];
          console.log(res.data.data[0].children);
          var devListArr = res.data.data[0].children;
          var dev;
          for (var i = 0; i < devListArr.length; i++) {
              dev = {
                name: devListArr[i].name,
                image: devListArr[i].originalUrl,
                modelId: devListArr[i].modelId,
                isShow: devListArr[i].publishFlag
              }
            devArr.push(dev);
          }
          that.setData({
            devList: devArr
          })
          wx.setStorage({
            key: 'devList',
            data: that.data.devList,
          })
        }



       

      }
    })


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 扫码添加
  getQRCode: function () {
    var that = this;
    var isShare=false;
    if (app.globalData.isLogin){
      wx.scanCode({        //扫描API
        success: function (res) {
          console.log(res);    //输出回调信息
          console.log(res.result);
          if (res.errMsg.indexOf("ok")>-1){
            that.setData({
              qRCodeMsg: res.result
            });
            if (res.result.length == 32) {
              that.setData({
                shareModelId: res.result
              })
              isShare = true;
            } else {
              isShare = false;
            }
            if(isShare){
              var timestamp = new Date().getTime();
              var param = {
                "sk": that.data.shareModelId
              }
              var params = JSON.stringify(param);
              wx.request({
                url: 'https://papi.zcyun.cn/v1/user/device/share/bind/' + that.data.shareModelId,
                method: "POST",
                data: {
                  "sk": that.data.shareModelId
                },
                header: {
                  'content-type': 'application/json', // 默认值
                  'openId': app.globalData.openId,
                  'ts': timestamp,
                  'sign': util.sha1("POST" + '/v1/user/device/share/bind/' + that.data.shareModelId + params + timestamp + app.globalData.openKey)
                },
                success(res) {
                  console.log(res.data);
                  if (res.data.code == 0) {
                    // 返回设备列表
                    wx.switchTab({
                      url: '../index/index'
                    })
                  }else{
                    wx.showToast({
                      icon:'none',
                      title: '二维码失效',
                      duration: 100000
                    })
                  }
                }
              })
            }else{
              var isHave=false;
              for(var i=0;i<that.data.devList.length;i++){
                if(res.result==that.data.devList[i].modelId){
                  isHave=true;
                }
              }
              if(isHave){
                wx.navigateTo({
                  url: '../peiWangShuoMing/peiWangShuoMing?qRCodeMsg=' + res.result,
                })
              }else{
                wx.showToast({
                  icon:'none',
                  title: '扫描不成功'
                })
              }
            }
          }
        }
      })
    }else{
      wx.showToast({
        title: '请先登录',
        icon: "none",
        duration: 2000
      })
    }
  },
  // 层级添加设备进入配网
  addDevBtn:function (e) {
    var that=this;
      var ind = e.currentTarget.dataset['index'];
      wx.navigateTo({
        url: '../peiWangShuoMing/peiWangShuoMing?name=' + that.data.devList[ind].name + '&qRCodeMsg=' + that.data.devList[ind].modelId,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    
  }

})